<?  include("../util.php");
	include("../config.inc.php");
	include("../clases.php");

global $conexion;

$examp = $_GET["q"]; //query number 
$id = $_GET['id']; // connect to the database 

//$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); 
//mysql_select_db($database) or die("Error conecting to db."); 


	$result = consultaSimple("lotes","fecha_baja IS NULL","","COUNT(*) AS count");
$count = $result['count']; 


$SQL = "SELECT id_lote, n_lote, cantidad, fecha_caducidad, id_distribuidor FROM lotes WHERE id_producto=".$id." ORDER BY fecha_caducidad"; 
//echo $SQL;	
$result = mysql_query( $SQL,$conexion ); 
/*
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) { 
		header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else { 
		header("Content-type: text/xml;charset=utf-8"); 
	} 
	
	$et1="<";
	$et = ">"; 
	//echo $et1.'?xml version="1.0" encoding="utf-8"?'.$et; 
	//include 'header.xml';
	$questionmark = "?";
$echo= "<".$questionmark."xml version=\"1.0\" encoding=\"utf-8\"".$questionmark.">";
	echo $echo." <rows>"; // be sure to put text data in CDATA 
	
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 
		echo "<row>"; echo "<cell>". $row[id_lote]."</cell>"; 
		echo "<cell><![CDATA[". $row[n_lote]."]]></cell>"; 
		echo "<cell>". $row[cantidad]."</cell>"; 
		echo "<cell>". $row[fecha_caducidad]."</cell>"; 
		echo "<cell>". $row[id_distribuidor]."</cell>"; 
	//	echo "<cell>". number_format($row[qty]*$row[unit],2,'.',' ')."</cell>"; 
		echo "</row>"; 
		} 
	echo "</rows>"; 
	
*/	
$campos = array('id_lote','n_lote','cantidad','fecha_caducidad','id_distribuidor');

		if( $count >0 ) { 
			$total_pages = 1; 
		} else { 
			$total_pages = 0;
		} 
		if ($page > $total_pages) 
			$page=$total_pages; 
		else 
			$page = 1;
//pasar_utf8_array($result);
		$responce->page = $page; 
		$responce->total = $total_pages; 
		$responce->records = $count; 

/*for($i=0;$i<count($result);$i++){
			$responce->rows[$i]['id']=$examp.":".$result[$i]["id_lote"]; 
			for($x=0;$x<count($campos);$x++)
				$responce->rows[$i]['cell'][]=$result[$i][$campos[$x]];
		}*/
			$i=0;
			while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 
				$responce->rows[$i]['id']=$row[id_lote];
				$responce->rows[$i]['cell'][]=$row[id_lote];
				$responce->rows[$i]['cell'][]=$row[n_lote];
				$responce->rows[$i]['cell'][]=$row[cantidad];
				$responce->rows[$i]['cell'][]=$row[fecha_caducidad];
				$responce->rows[$i]['cell'][]=$row[id_distribuidor];
				$i++;
			}

		echo json_encode($responce); 
		//die();
	
?>